Parsing JSON data

by: glennf, 7 years ago

Last edited: 7 years ago

I am taking data from an html table and posting it back to python so I can process user changes.
I get the data packaged and sent to Python and it looks like the below (just a flash of the variable).
Each record should really be a row with 9 elements so the first row should start at the beginning of the line and end at the 1 just before "test1". "test1" should start the next line and so on.  Not sure if I am not formatting it correctly in the HTML or if I am missing something in Python. Any guidance is appreciated. Code below:
wo name5 | Sticky_Tape_Catch_Behind_R-97.mp4 | cat1 | subcat1 | 200 | 0 | 1 | 0 | 1 test1 | https://youtu.be/P-61-sgSN3w | testcat1 | testsubcat1 | 20 | 0 | 1 | 0 | 0 egsegege | Outside_Pull_Outside_Pull-59.mp4 | fgsedsefgs | segsegse | 1 | 0 | 1 | 0 | 0 pdf | HTML-Cheat-Sheet_1.pdf | pdf cat | pdf sub cat | 20 | 0 | 1 | 0 | 0 pdf | HTML-Cheat-Sheet_2.pdf | pdf cat | pdf sub cat | 20 | 0 | 1 | 0 | 0
HTML <pre class='prettyprint lang-py'>
function tablePass()
{
                var html_table_data = "";  
                var bRowStarted = true;  
                $('#woTable tbody>tr').each(function () {  
                    $('td', this).each(function () {  
                        if (html_table_data.length == 0 || bRowStarted == true) {  
                            html_table_data += $(this).text();  
                            bRowStarted = false;  
                        }  
                        else  
                            html_table_data += " | " + $(this).text();  
                    });  
                    html_table_data += "n";  
                    bRowStarted = true;  
                });  
alert(html_table_data);
// var plantabledata = JSON.stringify(TableData);

$.ajax({
    url: '/plan_update/',
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify(html_table_data),
    type: 'POST',
    success: function(response) {
        console.log(response);
    },
    error: function(error) {
        console.log(error);
    }
});


}
</pre>
Python  ---  the second flash is never executed
<pre class='prettyprint lang-py'>
else:
                plantable = json.loads(request.data)
                flash(plantable)
                for row in plantable:  
                    #if row[8] != 0:
                        Time = row[5]
                        Quantity = row[6]
                        Reps = row[7]
                        Rest = row[8]
                        Seq = row[9]
                        flash("t"+time+"q"+Quantity+"r"+Reps+"r"+Rest+"s"+Seq)
                        return render_template("plan_update.html", plandata = plandata, selectdetail=selectdetail,planwodata=planwodata)
</pre>



You must be logged in to post. Please login or register an account.